package MapReduce.Demo9_Grouping_Grade;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * @Author lixinlei
 * @Date 2023/3/21 10:38
 */
public class GradeGroupingComparator extends WritableComparator {

    public GradeGroupingComparator() {
        super(Text.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {

        Text aBean = (Text)a;
        Text bBean = (Text)b;
        //获取两个学号
        String aStr = aBean.toString();
        String bStr = bBean.toString();

        //获取每个学生的年级和班级
        String aClass = aStr.substring(0,2);
        String bClass = bStr.substring(0,2);

        //定义排序规则作为自定义分组条件
        return aClass.compareTo(bClass);

    }
}
